// pages/pay/pay.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //图标
    iconPath: app.globalData.iconPath,
    //访问java后台
    goJavaBackstagePath: app.globalData.goJavaBackstagePath,
    //测试数据图片
    testDataImgPath: app.globalData.testDataImgPath,


    order:{},
    typeProductList:[],
    additionProductList:[],
    selectTypeId:1,
    selectProductId:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var orderId = options.orderId;

     //获取支付页面的数据
    // wx.request({
    //   url: this.data.goJavaBackstagePath+'/order/getPayPageData',
    //   data: { orderId: this.data.orderId},
    //   header: {
    //     'content-type': 'application/json'
    //   },
    //   success: function (res) {
    //     console.log(res.data)
    //   }
    // })

    this.setData({
      order: { id: 1, amountMoney: 8888, advanceCharge:1200,total:12888},
      additionProductList: [
        { id: 2, name: '相框产品二', price: 110, productNum: 1 },
        { id: 3, name: '相框产品三', price: 130, productNum: 2 },
        { id: 9, name: '相册产品三', price: 30, productNum: 1 },
        { id: 12, name: '钥匙扣产品三', price: 30, productNum: 1 },
      ],
      typeProductList: [{
        id: 1, name: '相框', products: [
          {
            id: 1, name: '相框产品一', price: 120, productNum: 2, photos: [
              { id: 1, url: this.data.testDataImgPath +'/mei1.jpg' }, { id: 2, url: this.data.testDataImgPath +'/mei2.jpg' }]
          },
          {
            id: 2, name: '相框产品二', price: 110, productNum: 2, photos: [
              { id: 1, url: this.data.testDataImgPath +'/mei1.jpg' }, { id: 2, url: this.data.testDataImgPath +'/mei2.jpg' },
              { id: 3, url: this.data.testDataImgPath +'/mei3.jpg' }, { id: 4, url: this.data.testDataImgPath +'/mei4.jpg' }]
          },
          {
            id: 3, name: '相框产品三', price: 130, productNum: 3, photos: [
              { id: 1, url: this.data.testDataImgPath +'/mei1.jpg' }, { id: 2, url: this.data.testDataImgPath +'/mei2.jpg' },
              { id: 3, url: this.data.testDataImgPath +'/mei3.jpg' }]
          },
        ]
      }, {
        id: 2, name: '海报', products: [
          {
            id: 4, name: '海报产品一', price: 80, productNum: 2, photos: [
              { id: 5, url: this.data.testDataImgPath +'/mei5.jpg' }, { id: 6, url: this.data.testDataImgPath +'/mei6.jpg' }]
          },
          {
            id: 5, name: '海报产品二', price: 90, productNum: 2,photos: [
              { id: 5, url: this.data.testDataImgPath +'/mei5.jpg' }, { id: 6, url: this.data.testDataImgPath +'/mei6.jpg' },
              { id: 7, url: this.data.testDataImgPath +'/mei7.jpg' }, { id: 8, url: this.data.testDataImgPath +'/mei8.jpg' },
              { id: 9, url: this.data.testDataImgPath +'/mei9.jpg' }, { id: 10, url: this.data.testDataImgPath +'/mei10.jpg' },]
          },
          {
            id: 6, name: '海报产品三', price: 100, productNum: 2,photos: [
              { id: 11, url: this.data.testDataImgPath +'/mei11.jpg' }, { id: 12, url: this.data.testDataImgPath +'/mei12.jpg' }]
          },
        ]
      }, {
        id: 3, name: '相册', products: [
          {
            id: 7, name: '相册产品一', price: 50, productNum: 1,photos: [
              { id: 13, url: this.data.testDataImgPath +'/mei13.jpg' }, { id: 14, url: this.data.testDataImgPath +'/mei14.jpg' },
              { id: 15, url: this.data.testDataImgPath +'/mei15.jpg' }, { id: 16, url: this.data.testDataImgPath +'/mei16.jpg' }]
          },
          {
            id: 8, name: '相册产品二', price: 60, productNum: 2,  photos: [
              { id: 17, url: this.data.testDataImgPath +'/mei17.jpg' }, { id: 18, url: this.data.testDataImgPath +'/mei11.jpg' },
              { id: 19, url: this.data.testDataImgPath +'/mei19.jpg' }, { id: 20, url: this.data.testDataImgPath +'/mei20.jpg' },
              { id: 21, url: this.data.testDataImgPath +'/mei21.jpg' }, { id: 22, url: this.data.testDataImgPath +'/mei22.jpg' }]
          },
          {
            id: 9, name: '相册产品三', price: 30, productNum: 1, photos: [
              { id: 23, url: this.data.testDataImgPath +'/mei23.jpg' }, { id: 24, url: this.data.testDataImgPath +'/mei24.jpg' },
              { id: 25, url: this.data.testDataImgPath +'/mei25.jpg' }, { id: 26, url: this.data.testDataImgPath +'/mei26.jpg' },
              { id: 27, url: this.data.testDataImgPath +'/mei27.jpg' }, { id: 28, url: this.data.testDataImgPath +'/mei28.jpg' }]
          },
        ]
      }, {
        id: 4, name: '钥匙扣', products: [
          {
            id: 10, name: '钥匙扣产品一', price: 10, productNum: 1,photos: [
              { id: 1, url: this.data.testDataImgPath +'/mei1.jpg' }, { id: 2, url: this.data.testDataImgPath +'/mei2.jpg' },
              { id: 3, url: this.data.testDataImgPath +'/mei3.jpg' }, { id: 4, url: this.data.testDataImgPath +'/mei4.jpg' }]
          },
          {
            id: 11, name: '钥匙扣产品二', price: 20, productNum: 1,photos: [
              { id: 17, url: this.data.testDataImgPath +'/mei17.jpg' }, { id: 18, url: this.data.testDataImgPath +'/mei11.jpg' },
              { id: 19, url: this.data.testDataImgPath +'/mei19.jpg' }]
          },
          {
            id: 12, name: '钥匙扣产品三', price: 30, productNum: 1,photos: [
              { id: 1, url: this.data.testDataImgPath +'/mei1.jpg' }]
          },
        ]
      }],
    });
  },

  openType:function(event){
    var typeid = event.target.dataset.typeid;
    for (var i = 0; i < this.data.typeProductList.length;i++){
      if (typeid == this.data.typeProductList[i].id){
        this.data.selectProductId = this.data.typeProductList[i].products[0].id;
      }
    }
    this.setData({
      selectTypeId:typeid,
      selectProductId: this.data.selectProductId,
    });
  },
  openProduct: function (event) {
    console.log('openProduct', event.target.dataset)
    this.setData({
      selectProductId: event.target.dataset.productid,
    });
  },
  triggerOpen:function(){},
  previewPhoto:function(event){
    var current=event.target.dataset.current;
    var urls=[];
    for (var i = 0; i < this.data.typeProductList.length;i++){
      if (this.data.selectTypeId == this.data.typeProductList[i].id){
        var typeProducts = this.data.typeProductList[i].products;
        for(var j=0;j<typeProducts.length;j++){
          if (this.data.selectProductId == typeProducts[j].id){
            for (var k=0;k<typeProducts[j].photos.length;k++){
              urls.push(typeProducts[j].photos[k].url);
            }
            break;
          }
        }
      }
    }
    wx.previewImage({current:current,urls:urls});
  },
//确认支付------------------------------
payMoney(event){
  var that = this;
  wx.showModal({
    title: '提示',
    content: '确认支付：订单将无法更改！',
    success: function (res) {
      if (res.confirm) {
        wx.request({
          url: '',//阅读  API后做：支付，订单状态改变到待支付   支付成功后状态改为： 待成片上传
          header: { 'content-type': 'application/json'},
          data:{},
          success: function (res) {
            console.log(res.data)
          }
        })
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    }
  })
}
})